<?php 
/**
 * name:miss
 * time:2017年10月11日18:53:25
 * qq:1312857376
 * message:第二天晚上  购物车逻辑页面
 */
namespace Home\Controller;
use Think\Controller;
/**
* 购物车类
*/
class CartController extends Controller
{
	//显示数据
	public function add_cart(){
		$cart_content = M('Cart') -> where(array('user_id'=>cookie('id'))) -> select();
		$this -> assign('cart_content',$cart_content);
		$this -> display();
	}

	public function add_cart1(){
		if(IS_POST){
			//获取商品信息
			$post =  I('post.');
			$post['user_id'] = cookie('id');
			//查询购物车是否存在当前商品
			$is_cart =  M('Cart') -> where($post) -> find();
			if($is_cart){
				//重定向地址，为了防止刷新页面提交表单
				redirect(U("add_cart"));
			}else{
				$goods_info = M("Goods") -> where(array('id'=>$post["goods_id"])) -> find();
				$post['goods_name']    = $goods_info['goods_name'];
				$post['goods_url']    = $goods_info['goods_image'];
				$post['goods_price']  = $goods_info['local_price'];
				$post['market_price'] = $goods_info['market_price'];
				$post['post_price'] = $goods_info['is_free_post'];
	
				M('Cart') -> add($post);
				//重定向地址，为了防止刷新页面提交表单
				redirect(U("add_cart"));
			}
		}else{
			//重定向地址，为了防止刷新页面提交表单
			redirect(U("add_cart"));
		}
	}

	public function del_cart()	
	{
		//删除购物车商品
		$cart_id =I('get.id');
		$result = M('Cart') ->where(array('user_id' => cookie('id'),'cart_id' => $cart_id))-> delete();
	}

	//ajax请求并录入订单
	public function enter(){

		//获取选中商品的id
		$cart_id = I("cart_select");
		$result = array();
		$n = 0;
		//将获取的购物车id进行遍历到新的数组中
		foreach($cart_id as $key => $value){
			$result[$n] = $key;
			$n++;
			echo $key;
		}
		//保存为订单
		foreach($result as $key => $value){
			M('Cart') -> is_order = 1;
			M('Cart') -> where("user_id = ".cookie('id')." and cart_id = $value") -> save();
		}
	}

	//ajax请求取消订单
	public function cancel_order(){
		$cart_id = I('get.cart_id');
		M('Cart') -> is_order = 0;
		M('Cart') -> where("user_id = ".cookie('id')." and cart_id = $cart_id") -> save();
	}

    //获取购物车表id,商品的数量
	public function get_num()
	{	
	
		$cart_id = I('get.cart_id');
		$goods_num = (int)I('get.goods_num');
		$goods_price = 	(int)M('Cart') -> where("cart_id = $cart_id ") -> find()['goods_price'];
		M('Cart') -> goods_num = $goods_num;
		M('Cart') -> where("cart_id = $cart_id ") -> save();
		echo $goods_num * $goods_price;
	}

	//获取被选中的总价钱
	public function get_total_price(){
		$all_data = M('Cart') -> where("user_id =".cookie('id')." and is_order = 1") -> select();
		$total_price = 0;
		foreach ($all_data as $key => $value) {
			$total_price += (int)$value["goods_num"] * (int)$value["goods_price"];
		}
		echo $total_price;die;
	}

	//全选
	public function checked_all(){
		$all_data = M('Cart') -> select();
		foreach ($all_data as $key => $value) {
			M('Cart') -> is_order = 1;
			M('Cart') -> where("user_id = ".cookie('id')." and cart_id =".$value['cart_id']) -> save();
		}
	}
	//全不选
	public function no_checked_all(){
		$all_data = M('Cart') -> select();
		foreach ($all_data as $key => $value) {
			M('Cart') -> is_order = 0;
			M('Cart') -> where("user_id = ".cookie('id')." and cart_id =".$value['cart_id']) -> save();
		}
	}

} 